//
//  WeatherView_Model.m
//  International_Hospital
//
//  Created by LiYiLin on 16/7/4.
//  Copyright © 2016年 LiYiLin. All rights reserved.
//
#import "WeatherView_Model.h"
#import "NetWorkDefine.h"
#import "NetWorkTool.h"

#import <AFHTTPSessionManager.h>


@implementation WeatherView_Model

+ (void)weatherWithCurrentCity:(NSString *)cityName finishBlock:(void (^)(WeatherView_Model *weatherModel))finishBlock errorBlock:(void (^)())errorBlock {
    
    /*
     请求地址：http://api.map.baidu.com/telematics/v3/weather
     请求参数：（city填什么，大家都懂的！）{@"location":city,@"output":@"json",@"ak":@"17IvzuqmKrK1cGwNL6VQebF9"};
     */
    // @"http://api.map.baidu.com/telematics/v3/weather?location=&#x4E0A;&#x6D77;&output=json&ak=17IvzuqmKrK1cGwNL6VQebF9"
    
    NSString *str = kGetWeather_URL(cityName);
    
    // 指定url字符串的某个部分进行转换  中文 转义
    str = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    [[NetWorkTool shareManager]GET_RequestWithUrlString:str parmas:nil successBloock:^(id responseObject) {
        NSArray *arr = responseObject[@"results"];
        
        // 如果 请求参数 提交了两个 或两个以上 城市 则返回对应个数的字典 存入数组里
        NSDictionary *cityDict = [arr firstObject];
        
        WeatherView_Model *model = [[WeatherView_Model alloc] init];
        
        // 先取出 城市 信息
        model.currentCity = cityDict[@"currentCity"];
        
        NSArray *weatherData = cityDict[@"weather_data"];
        
        // 数组中有最近几天的天气预报,只需要第一条
        NSDictionary *weatherDict = [weatherData firstObject];
        
        [model setValuesForKeysWithDictionary:weatherDict];
        
        // 回调 返回 天气 model
        finishBlock(model);
    } failureBlock:^(NSError *error) {
        
    }];
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key {}

@end

 












